﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts.Report;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Kingdee.K3.FIN.App.Core.Match.Object;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Kingdee.BOS.Core.Enums.BOSEnums;

namespace RK.ReportPlugin.BPW
{
    [Description("简单账表：生产领料汇总表"), HotUpdate]
    public class PickSumReport : SysReportBaseService
    {
        public override void Initialize()
        {
            base.Initialize();
            this.ReportProperty.ReportType = ReportType.REPORTTYPE_NORMAL;
            this.ReportProperty.ReportName = new LocaleValue("生产领料汇总表", base.Context.UserLocale.LCID);
            this.IsCreateTempTableByPlugin = true;
            this.ReportProperty.IsUIDesignerColumns = false;
            this.ReportProperty.IsGroupSummary = true;
            this.ReportProperty.SimpleAllCols = false;
            this.ReportProperty.PrimaryKeyFieldName = "FMatNumber";
            this.ReportProperty.IdentityFieldName = "FIDENTITYID";
        }
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            string period = "";
            string depNumber = "";
            string matNumber = "";
            string matNumber2 = "";
            DynamicObject customFil = filter.FilterParameter.CustomFilter;
            if (customFil != null)
            {
                period = customFil["F_POSW_ComboPeriod"].ToString();
                DynamicObject dep = customFil["F_POSW_BaseDep"] as DynamicObject;
                depNumber = dep == null ? "" : dep["Number"].ToString();
                DynamicObject mat = customFil["F_POSW_BaseProduct"] as DynamicObject;
                matNumber = mat == null ? "" : mat["Number"].ToString();
                DynamicObject mat2 = customFil["F_POSW_BaseProduct2"] as DynamicObject;
                matNumber2 = mat2 == null ? "" : mat2["Number"].ToString();
            }
            List<SqlParam> sqlParams = new List<SqlParam>();
            sqlParams.Add(new SqlParam("@TempName", KDDbType.AnsiString, tableName));
            sqlParams.Add(new SqlParam("@Period", KDDbType.AnsiString, period));
            sqlParams.Add(new SqlParam("@DepNumber", KDDbType.AnsiString, depNumber));
            sqlParams.Add(new SqlParam("@MatNumber", KDDbType.AnsiString, matNumber));
            sqlParams.Add(new SqlParam("@MatNumber2", KDDbType.AnsiString, matNumber2));
            DBUtils.ExecuteStoreProcedure(this.Context, "P_ZSLH_PickSumReport", sqlParams);
        }
        public override ReportHeader GetReportHeaders(IRptParams filter)
        {
            ReportHeader header = new ReportHeader();
            //header.AddChild("FIDENTITYID", new LocaleValue("序号"), SqlStorageType.SqlInt, visible: true);
            header.AddChild("FMatNumber", new LocaleValue("配件代码"), SqlStorageType.Sqlnvarchar, visible: true);
            header.AddChild("FMatName", new LocaleValue("配件名称"), SqlStorageType.Sqlnvarchar, visible: true);
            header.AddChild("FMatTH", new LocaleValue("配件图号"), SqlStorageType.Sqlnvarchar, visible: true);
            header.AddChild("FQTY", new LocaleValue("数量", 2052), SqlStorageType.SqlDecimal, visible: true);
            header.AddChild("FPrice", new LocaleValue("单价", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("FAMOUNT", new LocaleValue("金额", 2052), SqlStorageType.SqlDecimal);
            return header;
        }

        public override ReportTitles GetReportTitles(IRptParams filter)
        {
            ReportTitles titles = new ReportTitles();
            string period = "";
            string depNumber = "";
            string matNumber = "";
            DynamicObject customFil = filter.FilterParameter.CustomFilter;
            if (customFil != null)
            {
                period = customFil["F_POSW_ComboPeriod"].ToString();
                DynamicObject dep = customFil["F_POSW_BaseDep"] as DynamicObject;
                depNumber = dep == null ? "" : dep["FullName"].ToString();
                DynamicObject mat = customFil["F_POSW_BaseProduct"] as DynamicObject;
                matNumber = mat == null ? "" : mat["Number"].ToString();
            }
            titles.AddTitle("F_POSW_Period", period);
            //titles.AddTitle("F_POSW_MatNumber", matNumber);
            titles.AddTitle("F_POSW_DepNumber", depNumber);
            return titles;
        }
        public override List<Kingdee.BOS.Core.Report.SummaryField> GetSummaryColumnInfo(IRptParams filter)
        {
            var result = base.GetSummaryColumnInfo(filter);
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("FAMOUNT", Enu_SummaryType.SUM));
            return result;
        }
    }
}
